package cn.tedu.medicalmanagementsystem.base.interceptors;

import cn.tedu.medicalmanagementsystem.escort.pojo.vo.EscortVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
public class EscortLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("进入陪诊员登录状态校验的拦截器...");
        /*
            1.获取session会话对象:HttpSession session = request.getSession();;
            2.从session中获取用户标识userVO;
            3.判断用户标识userVO是否为null;
              3.1 不为null: 放行;
              3.2 为null: 拦截,重定向到登录页面：response.sendRedirect("/login.html");
         */
        HttpSession session = request.getSession();
        EscortVO escortVO = (EscortVO) session.getAttribute("escort");
        if (escortVO == null){
            // 拦截,重定向到登录页面 http://localhost:8080/login.html
            response.sendRedirect("http://localhost:9091");
            return false;
        }
        //放行
        return true;
    }
}
